﻿using aliyun_net_sdk_core_extension;
using aliyun_net_sdk_core_extension.Client;
using Autofac;
using Autofac.Builder;
using Autofac.Extensions.DependencyInjection;
using CommonTest.Helper;
using CommTest;
using CommTest.Generic;
using CommTest.Helper;
using ElasticSearchTest;
using JiebaNet.Analyser;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using NPOITest;
using System.Threading.Tasks;
using CommTest.T4;
using PuvBuilder;
using System.Diagnostics;

namespace CommonTest
{

    class Program
    {

        public static AutofacServiceProvider ServiceProvider { get; private set; }

        static void Main(string[] args)
        {
            new Builder(new Options() { 
                DefaultSqlServerConnectionString = "Server=192.168.31.13;database=iSchoolArticle;user id=iSchool;password=SxbLucas$0769;MultipleActiveResultSets=true;" 
            }).Run();

            var nameof1 = nameof(Program.ServiceProvider);
            Debug.Assert(nameof1 == "ServiceProvider");
            var user = new User();
            var nameof2 = nameof(user.Id);
            Debug.Assert(nameof2 == "Id");

            new HttpTest().Test();

            while(true) { }
            return;
            new TaskTest().Test();
            new TestDefault().Test();
            new ParallelTest().Test();
            TryTest.Test();
            GCTest.Test();
            UriTest.Test();
            Func<int, Task> action = async mnumb =>
            {
                await Task.Delay(TimeSpan.FromSeconds(3));
                Console.WriteLine(mnumb);
            };

            var result = Task.FromResult(action).GetAwaiter().GetResult();


            var asyncAttr = typeof(System.Runtime.CompilerServices.AsyncStateMachineAttribute);
            if (action.Method.GetCustomAttributes(asyncAttr, true) != null)
            {
                //netcore 不支持
                //IAsyncResult result = action.BeginInvoke(2, ar => {}, null);
                //action.EndInvoke(result);

                //action.Invoke(2).GetAwaiter().GetResult();
            }
            T4Code.Test();

            StringTest.Test();

            BuildClass.Test();

            MathTest.Test();

            //ImplTest.Test();

            ReflectClass.Test();
            ReflectClass.Build();

            ReadText.Test();

            //ExcelTest.Test();

            int? inttest = null;
            var bo = inttest == 0;
            bool boo = inttest.HasValue;

            DigitBaseConvert.ToSence(Guid.NewGuid());

            EnumTest.Test();
            //var d = sizeof("123");

            var md5Str = "123";
            var data = MD5.Create(md5Str);
            var data2 = MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(md5Str));
            var data2Str = BitConverter.ToString(data2);
            var no = UrlShortIdUtil.Base322Long("4v77n");

            DI();

            //LinqTest.Test();
            ServiceProvider.GetService<Builder>().Run();

            ServiceProvider.GetService<SyncElasticsearchData>().Sync().GetAwaiter().GetResult();
            ServiceProvider.GetService<CdnLogTest>().Test().GetAwaiter().GetResult();
            ServiceProvider.GetService<EsTest>().Test();

            //AliyunTest.Test();
            //RegexTest.Build();
            //UrlTest.Test();
            //AutoPublishTopic.Build();

            var phone = "15521034082";
            var encryption = HideNumber(phone);

            Console.WriteLine("Hello World!");

            Console.ReadLine();
        }

        public static IEnumerable<User> TestNullEnumerable()
        {
            yield break;
        }

        public static string HideNumber(string number)
        {
            if (string.IsNullOrEmpty(number))
            {
                return null;
            }
            else
            {
                int s = number.Length * 3 / 11;
                int e = number.Length * 7 / 11;
                int x = number.Length - s;
                return number.Substring(0, s) + number.Substring(e).PadLeft(x, '*');
            }
        }

        public static void DI()
        {
            var serviceCollection = new ServiceCollection();

            serviceCollection.AddPuvBuilder(options => options.ConfigSqlServer("Server=192.168.31.13;database=iSchoolArticle;user id=iSchool;password=SxbLucas$0769;MultipleActiveResultSets=true;"));

            var containerBuilder = new ContainerBuilder();
            containerBuilder.Populate(serviceCollection);
            //containerBuilder.RegisterType<EsClient>();
            //containerBuilder.RegisterType<UserEsService>();
            //containerBuilder.RegisterType<EsTest>();
            containerBuilder.RegisterType<SyncElasticsearchData>();
            containerBuilder.RegisterType<CdnLogTest>();

            //var currentingLib = Assembly.GetExecutingAssembly();
            //containerBuilder.RegisterAssemblyTypes(currentingLib);

            //AssemblyName.GetAssemblyName
            var esLib = Assembly.LoadFrom("ElasticSearchTest");
            containerBuilder.RegisterAssemblyTypes(esLib);


            var aliyunLib = Assembly.LoadFrom("aliyun-net-sdk-core-extension");
            containerBuilder.RegisterAssemblyTypes(aliyunLib).AsSelf().AsImplementedInterfaces();

            var container = containerBuilder.Build();
            var serviceProvider = new AutofacServiceProvider(container);
            ServiceProvider = serviceProvider;
        }

    }
}
